import cv2
import numpy as np





def nothing(x):
    pass
#鼠标回调函数 
#事件 坐标 标志 返回
def show_num(event,x,y,flags,param):
    if event==cv2.EVENT_LBUTTONUP:
        print('POS:(',x,y,') R:',r,'G:',g,'B:',b)
        cv2.circle(img,(x,y),10,(0,0,0),-1)
    elif event==cv2.EVENT_MBUTTONDOWN:
        print(num)
# 创建一副黑色图像
img=np.zeros((300,512,3),np.uint8)
cv2.namedWindow('image')

cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)
switch='0:OFF\n1:ON'
cv2.createTrackbar(switch,'image',0,1,nothing)
cv2.createTrackbar('value','image',0,255,nothing)
cv2.setMouseCallback('image',show_num)
while(1):
    #print(color_read)
    cv2.imshow('image',img)
    k=cv2.waitKey(1)&0xFF
    if k==27:
        break
    r=cv2.getTrackbarPos('R','image')
    g=cv2.getTrackbarPos('G','image')
    b=cv2.getTrackbarPos('B','image')
    num=cv2.getTrackbarPos('value','image')
    s=cv2.getTrackbarPos(switch,'image')
    if s!=0:
        img[:]=0
    else:
        img[:]=[b,g,r]
cv2.destroyAllWindows()